﻿using Black.Minecraft.Editor.ViewModels.Core;
using Black.NBT;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace Black.Minecraft.Editor.ViewModels.DataModels
{
    /// <summary>
    /// DataModel pour les enchantements
    /// </summary>
    public class EnchantDataModel : BaseViewModel
    {
        #region Propriétés

        /// <summary>
        /// Obtient la commande de fermeture
        /// </summary>
        public ICommand CloseCommand
        {
            get
            {
                return (this._closeCommand);
            }
        }

        /// <summary>
        /// Obtient ou définit le nom de l'élément
        /// </summary>
        public string DisplayName
        {
            get
            {
                if (this._data.Contains("display", TagType.Compound))
                    return (((TagString)((TagCompound)this._data["display"])["Name"]).Value);

                return (null);
            }
            set
            {
                string oldValue = null;

                if (this._data.Contains("display", TagType.Compound))
                    oldValue = ((TagString)((TagCompound)this._data["display"])["Name"]).Value;

                if (oldValue != value)
                {
                    if (string.IsNullOrEmpty(value))
                    {
                        if (this._data.Contains("display", TagType.Compound))
                            this._data.Items.Remove(this._data["display"]);
                    }
                    else
                    {
                        if (!this._data.Contains("display", TagType.Compound))
                        {
                            TagCompound display = new TagCompound("display");
                            display.Items.Add(new TagString("Name", value));
                            this._data.Items.Add(display);
                        }
                        else
                            ((TagString)((TagCompound)this._data["display"])["Name"]).Value = value;
                    }

                    this.OnPropertyChanged();
                    this.OnPropertyChanged("ToolTip");
                    MainViewModel.Instance.IsModified = true;
                }
            }
        }

        /// <summary>
        /// Obtient la liste des enchantements sur l'objet
        /// </summary>
        public ObservableCollection<EnchantItemDataModel> Items
        {
            get
            {
                return(this._items);
            }
        }

        /// <summary>
        /// Obtient ou définit le coût de réparation
        /// </summary>
        public int RepairCost
        {
            get
            {
                if (this._data.Contains("RepairCost", TagType.Int))
                    return ((this._data["RepairCost"] as TagInt).Value);

                return (0);
            }
            set
            {
                int oldValue = 0;

                if (this._data.Contains("RepairCost", TagType.Int))
                    oldValue = ((TagInt)this._data["RepairCost"]).Value;

                if (oldValue != value)
                {
                    if (value < 1)
                    {
                        if (this._data.Contains("RepairCost", TagType.Int))
                            this._data.Items.Remove(this._data["RepairCost"]);
                    }
                    else
                    {
                        if (!this._data.Contains("RepairCost", TagType.Int))
                        {
                            TagInt repaircost = new TagInt("RepairCost", value);
                            this._data.Items.Add(repaircost);
                        }
                        else
                            ((TagInt)this._data["RepairCost"]).Value = value;
                    }

                    this.OnPropertyChanged();
                    this.OnPropertyChanged("ToolTip");
                    MainViewModel.Instance.IsModified = true;
                }
            }
        }

        /// <summary>
        /// Obtient le tootip de l'item
        /// </summary>
        public string ToolTip
        {
            get
            {
                string tooltip = string.Empty;

                // Création de la chaîne

                if (this.RepairCost > 0)
                    tooltip += string.Format(" (RC: {0})", this.RepairCost);

                if  (this._items.Count >0 )
                {
                    tooltip += "\n";

                    foreach(EnchantItemDataModel item in this._items)
                        tooltip += string.Format("\n{0}", item.ToolTip);
                }

                // On retourne la valeur

                return (tooltip);
            }
        }

        #endregion

        #region Variables d'instances

        private TagCompound _data;
        private ObservableCollection<EnchantItemDataModel> _items;

        private RelayCommand _closeCommand;

        #endregion

        #region Constructeurs

        /// <summary>
        /// Constructeu de la classe
        /// </summary>
        /// <param name="data">Source de donnée</param>
        public EnchantDataModel()
            : base()
        {
            this._data = new TagCompound("tag");
            this._data.Items.Add(new TagInt("RepairCost", 2));
            this._items = new ObservableCollection<EnchantItemDataModel>();
        }

        /// <summary>
        /// Constructeu de la classe
        /// </summary>
        /// <param name="data">Source de donnée</param>
        public EnchantDataModel(TagCompound data)
            : base()
        {
            this._data = data;
            this._items = new ObservableCollection<EnchantItemDataModel>();

            // Chargement des enchantements

            if ( this._data.Contains("ench", TagType.List))
            {
                TagList list = (TagList)this._data["ench"];

                foreach(TagCompound enchant in list.Items)
                    this._items.Add(new EnchantItemDataModel(enchant));
            }
        }

        #endregion

        #region Fonctions publiques

        /// <summary>
        /// Retourne la source de donnée
        /// </summary>
        /// <returns></returns>
        public TagCompound GetData()
        {
            return (this._data);
        }

        #endregion

        #region Fonctions protégées

        /// <summary>
        /// Initialise les commandes
        /// </summary>
        protected override void OnInitializeCommand()
        {
            this._closeCommand = new RelayCommand(this.OnCloseCommandExecute);
        }

        #endregion

        #region Fonctions privées

        /// <summary>
        /// Se produit sur la commande de fermeture
        /// </summary>
        /// <param name="parameter">Paramètre de la commande</param>
        private void OnCloseCommandExecute(object parameter)
        {
            MainViewModel.Instance.ClosePopup();
        }

        #endregion
    }
}
